-
Notifications
You must be signed in to change notification settings - Fork 14
Modify test and utils.py to fix flaky test #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
pv8
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blazyy, thank you for your contribution! I've added a suggestion, please take a look.
|
|
||
|
|
||
| def get_ip(): | ||
| def get_ip(httpbin_url): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this method is being used in other parts of the code (I need to improve the tests in general 😞), what about attributing a default value to the new function parameter?
| def get_ip(httpbin_url): | |
| def get_ip(req_ip_url=HTTPBIN_URL): |
|
|
||
| def test_get_ip(self): | ||
| ip = utils.get_ip() | ||
| ip = utils.get_ip('https://httpbin.org/ip') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, take a look at the above comment.
| ip = utils.get_ip('https://httpbin.org/ip') | |
| ip = utils.get_ip() |
| utils.HTTPBIN_URL = 'http://example.nothing' | ||
|
|
||
| ip = utils.get_ip() | ||
| ip = utils.get_ip('http://example.nothing') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, take a look at the above comment.
| ip = utils.get_ip('http://example.nothing') | |
| ip = utils.get_ip(req_ip_url='http://example.nothing') |
The test
test_get_ip()under theSanityTestclass insidetest_noipy.pywas found to be flaky, i.e. a test that both passes and fail despite no changes to the code or the tests itself.Using the pytest-flakefinder plugin, when the test was re-run more than once, it was failing. Upon looking at the code, the reason was because the the value of
HTTPBIN_URLinsideutils.pywas not being set back to its default value ofhttps://httpbin.org/ipTo fix it, I just changed the
test_get_ip()test and have it be provided with the HTTPBIN_URL value instead of the value being read from the global variable.To reproduce flakiness:
pytest --flake-finder test/test_noipy.py::SanityTest::test_get_ipAfter implementing the fix, all tests pass successfully, both with and without the pytest-flakefinder plugin being used.
In general, flaky tests are a pain to fix with regards to locating the root of the actual problem, so it's a good idea to fix them when you detect them. I'm aware that the tests might not be re-run at all, but this change makes the entire module more robust and future-proof so it's just a small fix for you to consider.